#include <iostream>
#include "Reactor.hpp"
#include "Listener.hpp"
#include "HandleConnection.hpp"
#include "PackageParse.hpp"

// ./selectServer port
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cerr << "Usage Error: \n\t" << argv[0] << " port" << std::endl;
        return -1;
    }

    EnableScreen();
    uint16_t port = std::atoi(argv[1]);

    HandleConnection hc(PackageParse::Parse);
    Listener listen(port, hc);

    std::unique_ptr<Reactor> svr = std::make_unique<Reactor>();

    // 将Listen连接加入svr中
    svr->AddConnection(listen.SockFd(), EPOLLIN | EPOLLET,
                       std::bind(&Listener::Accept, &listen, std::placeholders::_1), nullptr, nullptr);

    // svr->AddConnection(listen.SockFd(), EPOLLIN, std::bind(&Listener::Accept, &listen, std::placeholders::_1), nullptr, nullptr);

    // 因为这里使用epoll的ET模式，要进行循环读取read，同时避免阻塞，需要将fd设置为非阻塞(这里直接在创建socket时将fd设置为非阻塞，accept也是如此)

    svr->Dispatcher();

    return 0;
}